'use strict';

console.log('foreground.js is running!');

// setInterval(() => {
//   console.log('消息我发了');
//   console.log('get-user-data' + Math.random());
//   // 1. Send a message to the service worker requesting the user's data
//   chrome.runtime.sendMessage('get-user-data' + Math.random(), (response) => {
//     // 3. Got an asynchronous response with the data from the service worker
//     console.log('received user data', response);
//   });
// }, 3000);

let s = document.createElement('script');
s.src = chrome.runtime.getURL('script.js');
s.onload = function() { this.remove(); };
(document.head || document.documentElement).appendChild(s);

function setItem(key, value) {
  localStorage.setItem(key, value);
}

chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
  console.log('changeFilterSwitch message received', request);
  if (request.action === "changeFilterSwitch") {
    let filterSwitch = localStorage.getItem('filterSwitch');
    if (!filterSwitch) {
      filterSwitch = 'true';
    }
    setItem('filterSwitch', filterSwitch === 'true' ? 'false' : 'true');
  }
});